package com.yanq.liancar.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.TabsPagerAdapter;
import com.yanq.liancar.fragment.CommentFragment;
import com.yanq.liancar.util.Constant;
import com.yanq.liancar.util.DensityUtil;
import com.yanq.liancar.util.ScreenUtil;
import com.yanq.liancar.util.Utils;
import com.yanq.liancar.view.tabs.TabsIndicator;

import java.util.ArrayList;
import java.util.List;

public class FindMessageActivity extends BaseToolbarActivity implements ViewPager.OnPageChangeListener {

    private RelativeLayout indicatorLine;
    private View indicator;
    private List<Fragment> fragmentList;

    public static void start(Context context) {
        context.startActivity(new Intent(context, FindMessageActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setStatusBar(this, true,false);
        setContentView(R.layout.activity_find_message);
        mTvTitle.setText("我的发现");

        TabsIndicator orderTabs = (TabsIndicator) findViewById(R.id.find_tabs);
        ViewPager orderPager = (ViewPager) findViewById(R.id.find_pager);
        indicatorLine = (RelativeLayout) findViewById(R.id.indicator_line);
        indicator = findViewById(R.id.indicator);
        fragmentList = new ArrayList<>();
        fragmentList.add(CommentFragment.newInstance(Constant.FIND_COMM));
        fragmentList.add(CommentFragment.newInstance(Constant.FIND_LIKE));
        orderPager.setAdapter(new TabsPagerAdapter(getSupportFragmentManager(), fragmentList));
        orderPager.addOnPageChangeListener(this);
        orderPager.setOffscreenPageLimit(2);
        orderTabs.setViewPager(orderPager);

        initIndicator();
    }

    private void initIndicator() {
        indicatorLine.getLayoutParams().width = ScreenUtil.getScreenWidth(mContext) / fragmentList.size();
        indicatorLine.requestLayout();
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
        );
        int interval = DensityUtil.dp2px(mContext, 60);
        params.setMargins(interval, 0, interval, 0);
        indicator.setLayoutParams(params);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        float offsetX = positionOffset * indicatorLine.getWidth();
        int startX = position * indicatorLine.getWidth();
        float translationX = startX + offsetX;
        ViewCompat.setTranslationX(indicatorLine, translationX);
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}